package org.study.tdengine.mybatisplus.boot.service;

import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.study.tdengine.mybatisplus.boot.mapper.TdengineMapper;

import java.util.*;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年10月17日
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class TdengineServiceImpl implements TdengineService {
    private final TdengineMapper tdengineMapper;

    @Override
    public List<Map<String, Object>> selectDianBiao1() {
        StopWatch stopWatch = new StopWatch("同一张表使用union all查询");
        stopWatch.start("查询");
        List<Map<String, Object>> list = tdengineMapper.selectDianBiao1();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        return list;
    }

    @Override
    public List<Map<String, Object>> selectDianBiao2() {
        List<Map<String, Object>> list = Lists.newArrayList();
        StopWatch stopWatch = new StopWatch("查所有列数据，然后在内存中组装列表");
        stopWatch.start("查询");
        Map<String, Object> map = tdengineMapper.selectDianBiao2();
        stopWatch.stop();
        stopWatch.start("组装数据");
        Map<String, Object> dataMap1 = new HashMap<>() {{
            this.put("time", map.getOrDefault("time", null));
            this.put("测量点", "光伏出线");
            this.put("正向有功总", map.getOrDefault("25601", null));
            this.put("反向有功总", map.getOrDefault("25606", null));
            this.put("正向无功总", map.getOrDefault("25611", null));
            this.put("反向无功总", map.getOrDefault("25616", null));
        }};
        list.add(dataMap1);
        Map<String, Object> dataMap2 = new HashMap<>() {{
            this.put("time", map.getOrDefault("time", null));
            this.put("测量点", "计量");
            this.put("正向有功总", map.getOrDefault("25621", null));
            this.put("反向有功总", map.getOrDefault("25626", null));
            this.put("正向无功总", map.getOrDefault("25631", null));
            this.put("反向无功总", map.getOrDefault("25646", null));
        }};
        list.add(dataMap2);
        Map<String, Object> dataMap3 = new HashMap<>() {{
            this.put("time", map.getOrDefault("time", null));
            this.put("测量点", "光伏进线");
            this.put("正向有功总", map.getOrDefault("25641", null));
            this.put("反向有功总", map.getOrDefault("25646", null));
            this.put("正向无功总", map.getOrDefault("25651", null));
            this.put("反向无功总", map.getOrDefault("25656", null));
        }};
        list.add(dataMap3);
        Map<String, Object> dataMap4 = new HashMap<>() {{
            this.put("time", map.getOrDefault("time", null));
            this.put("测量点", "SVG");
            this.put("正向有功总", map.getOrDefault("25661", null));
            this.put("反向有功总", map.getOrDefault("25666", null));
            this.put("正向无功总", map.getOrDefault("25671", null));
            this.put("反向无功总", map.getOrDefault("25676", null));
        }};
        list.add(dataMap4);
        Map<String, Object> dataMap5 = new HashMap<>() {{
            this.put("time", map.getOrDefault("time", null));
            this.put("测量点", "晋煤关口");
            this.put("正向有功总", map.getOrDefault("25681", null));
            this.put("反向有功总", map.getOrDefault("25686", null));
            this.put("正向无功总", map.getOrDefault("25691", null));
            this.put("反向无功总", map.getOrDefault("25696", null));
        }};
        list.add(dataMap5);
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        return list;
    }
}
